import 'package:flutter/cupertino.dart';
import 'package:extended_image/extended_image.dart';
import 'package:emby/util/function.dart';
import 'package:flutter/material.dart';

class CupertinoExtendedImage extends StatelessWidget {
  final String url;
  final BoxFit fit;
  final double width;
  final double height;
  final BoxShape boxShape;
  final BorderRadius borderRadius;
  final Widget placeholder;
  CupertinoExtendedImage(
    this.url, {
    this.fit,
    this.width,
    this.height,
    this.boxShape: BoxShape.rectangle,
    this.borderRadius: BorderRadius.zero,
    this.placeholder,
  });
  @override
  Widget build(BuildContext context) {
    return Hero(
      tag: url,
      child: ExtendedImage.network(
        url,
        fit: fit,
        width: width,
        cache: true,
        height: height,
        shape: boxShape,
        borderRadius: borderRadius,
        loadStateChanged: (ExtendedImageState state) {
          switch (state.extendedImageLoadState) {
            case LoadState.loading:
              return CupertinoActivityIndicator();
              break;
            case LoadState.completed:
              return ExtendedRawImage(
                image: state.extendedImageInfo?.image,
                width: width,
                height: height,
                fit: fit,
              );
              break;
            case LoadState.failed:
              return placeholder != null
                  ? placeholder
                  : GestureDetector(
                      onTap: () {
                        state.reLoadImage();
                      },
                      child: Container(
                        color: Colors.grey,
                        alignment: Alignment.center,
                        child: Icon(
                          CupertinoIcons.refresh_thick,
                          color: CupertinoColors.black,
                        ),
                      ),
                    );
              break;
            default:
              return Container();
          }
        },
      ),
    );
  }
}
